#include <stdio.h>
#include <stdlib.h>

struct xLIST_ITEM 
{
    unsigned int xItemValue;
    struct xLIST_ITEM * pxNext;
    struct xLIST_ITEM * pxPrevious;
    void * pvOwner;
    void * pvContainer;
};
typedef struct xLIST_ITEM ListItem_t;

struct xMINI_LIST_ITEM
{
    unsigned int xItemValue;
    struct xLIST_ITEM * pxNext;
    struct xLIST_ITEM * pxPrevious;
};
typedef struct xMINI_LIST_ITEM MiniListItem_t;

typedef struct xLIST
{
    unsigned long uxNumberOfItems;
    ListItem_t * pxIndex;
    MiniListItem_t xListEnd;
}List_t;

void vListInitialise(List_t * const pxList)
{
	printf("pxList = %p\n", pxList);

	printf("pxList->pxIndex = %p, &(pxList->xListEnd) = %p\n", pxList->pxIndex, &(pxList->xListEnd));
    pxList->pxIndex = (ListItem_t *) &(pxList->xListEnd);
	printf("pxList->pxIndex = %p, &(pxList->xListEnd) = %p\n", pxList->pxIndex, &(pxList->xListEnd));

    pxList->xListEnd.xItemValue = 0xFFFFFFFF;

	printf("pxList->xListEnd.pxNext = %p\n", pxList->xListEnd.pxNext);
    pxList->xListEnd.pxNext = (ListItem_t *) &(pxList->xListEnd);
	printf("pxList->xListEnd.pxNext = %p\n", pxList->xListEnd.pxNext);

	printf("pxList->xListEnd.pxPrevious = %p\n", pxList->xListEnd.pxPrevious);
    pxList->xListEnd.pxPrevious = (ListItem_t *) &(pxList->xListEnd);
	printf("pxList->xListEnd.pxPrevious = %p\n", pxList->xListEnd.pxPrevious);

	/* 形成回形，因此无论多少个pxNext、pxPrevious，地址都一样。以下是各种组合的地址打印 
	   xListEnd.pxPrevious->pxNext */
	printf("pxList->xListEnd.pxPrevious->pxNext = %p\n", pxList->xListEnd.pxPrevious->pxNext);
	printf("pxList->xListEnd.pxPrevious->pxNext->pxNext = %p\n", pxList->xListEnd.pxPrevious->pxNext->pxNext);
	printf("pxList->xListEnd.pxPrevious->pxNext->pxNext->pxNext = %p\n\n", pxList->xListEnd.pxPrevious->pxNext->pxNext->pxNext);

	/* xListEnd.pxPrevious->pxPrevious */
	printf("pxList->xListEnd.pxPrevious->pxPrevious = %p\n", pxList->xListEnd.pxPrevious->pxPrevious);
	printf("pxList->xListEnd.pxPrevious->pxPrevious->pxPrevious = %p\n", pxList->xListEnd.pxPrevious->pxPrevious->pxPrevious);
	printf("pxList->xListEnd.pxPrevious->pxPrevious->pxPrevious->pxPrevious = %p\n\n", pxList->xListEnd.pxPrevious->pxPrevious->pxPrevious->pxPrevious);

	/* xListEnd.pxNext->pxNext */
	printf("pxList->xListEnd.pxNext->pxNext = %p\n", pxList->xListEnd.pxNext->pxNext);
	printf("pxList->xListEnd.pxNext->pxNext->pxNext = %p\n", pxList->xListEnd.pxNext->pxNext->pxNext);
	printf("pxList->xListEnd.pxNext->pxNext->pxNext->pxNext = %p\n\n", pxList->xListEnd.pxNext->pxNext->pxNext->pxNext);

	/* xListEnd.pxNext->pxPrevious */
	printf("pxList->xListEnd.pxNext->pxPrevious = %p\n", pxList->xListEnd.pxNext->pxPrevious);
	printf("pxList->xListEnd.pxNext->pxPrevious->pxPrevious = %p\n", pxList->xListEnd.pxNext->pxPrevious->pxPrevious);
	printf("pxList->xListEnd.pxNext->pxPrevious->pxPrevious->pxPrevious = %p\n\n", pxList->xListEnd.pxNext->pxPrevious->pxPrevious->pxPrevious);

	/* xListEnd.pxNext->pxPrevious->pxNext */
	printf("pxList->xListEnd.pxNext->pxPrevious->pxNext = %p\n", pxList->xListEnd.pxNext->pxPrevious->pxNext);
	printf("pxList->xListEnd.pxNext->pxPrevious->pxNext->pxPrevious = %p\n\n", pxList->xListEnd.pxNext->pxPrevious->pxNext->pxPrevious);

	/* xListEnd.pxPrevious->pxNext->pxPrevious */
	printf("pxList->xListEnd.pxPrevious->pxNext->pxPrevious = %p\n", pxList->xListEnd.pxPrevious->pxNext->pxPrevious);
	printf("pxList->xListEnd.pxPrevious->pxNext->pxPrevious->pxNext = %p\n\n", pxList->xListEnd.pxPrevious->pxNext->pxPrevious->pxNext);

    pxList->uxNumberOfItems = (unsigned long) 0U;
}

void vListInsertEnd(List_t * const pxList, ListItem_t * const pxNewListItem)
{
    ListItem_t * const pxIndex = pxList->pxIndex;

	printf("pxIndex = %p, pxList->pxIndex = %p, pxNewListItem = %p\n", pxIndex, pxList->pxIndex, pxNewListItem);

	printf("pxNewListItem->pxNext = %p\n", pxNewListItem->pxNext);
    pxNewListItem->pxNext = pxIndex;
	printf("pxNewListItem->pxNext = %p, pxIndex = %p\n", pxNewListItem->pxNext, pxIndex);

	printf("pxNewListItem->pxPrevious = %p\n", pxNewListItem->pxPrevious);
    pxNewListItem->pxPrevious = pxIndex->pxPrevious;
	printf("pxNewListItem->pxPrevious = %p, pxIndex->pxPrevious = %p\n", pxNewListItem->pxPrevious, pxIndex->pxPrevious);

	printf("pxIndex->pxPrevious->pxNext = %p\n", pxIndex->pxPrevious->pxNext);
    pxIndex->pxPrevious->pxNext = pxNewListItem;
	printf("pxIndex->pxPrevious->pxNext = %p\n", pxIndex->pxPrevious->pxNext);

	printf("pxIndex->pxPrevious = %p\n", pxIndex->pxPrevious);
    pxIndex->pxPrevious = pxNewListItem;
	printf("pxIndex->pxPrevious = %p\n\n", pxIndex->pxPrevious);

    pxNewListItem->pvContainer = (void *) pxList;

    (pxList->uxNumberOfItems)++;
}

void vListInsert(List_t * const pxList, ListItem_t * const pxNewListItem)
{
    ListItem_t * pxIterator;
    const unsigned int xValueOfInsertion = pxNewListItem->xItemValue;

    if (xValueOfInsertion == 0xFFFFFFFF) {
        pxIterator = pxList->xListEnd.pxPrevious;
    }
    else {
        for (pxIterator = (ListItem_t *) &(pxList->xListEnd); 
             pxIterator->pxNext->xItemValue <= xValueOfInsertion;
             pxIterator = pxIterator->pxNext) {
            /* There is nothing to do here, just iterating to the wanted
               insertion position. */
            }
    }

    pxNewListItem->pxNext = pxIterator->pxNext;
    pxNewListItem->pxNext->pxPrevious = pxNewListItem;
    pxNewListItem->pxPrevious = pxIterator;
    pxIterator->pxNext = pxNewListItem;

    pxNewListItem->pvContainer = (void *) pxList;

    (pxList->uxNumberOfItems)++;
}

unsigned int uxListRemove(ListItem_t * const pxItemToRemove)
{
    List_t * const pxList = (List_t *) pxItemToRemove->pvContainer;

    pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious;
    pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext;

    if (pxList->pxIndex == pxItemToRemove) {
        pxList->pxIndex = pxItemToRemove->pxPrevious;
    }

    pxItemToRemove->pvContainer = NULL;
    pxList->uxNumberOfItems--;

    return pxList->uxNumberOfItems;
}

void main()
{
    List_t ac = {0}, * pac;

    ListItem_t new = {
        .xItemValue = 123,
        .pxNext = NULL,
        .pxPrevious = NULL,
        .pvOwner = NULL,
    };

	ListItem_t new2 = {
        .xItemValue = 456,
        .pxNext = NULL,
        .pxPrevious = NULL,
        .pvOwner = NULL,
    };

	ListItem_t new3 = {
        .xItemValue = 789,
        .pxNext = NULL,
        .pxPrevious = NULL,
        .pvOwner = NULL,
    };

	printf("ac = %p, new = %p, new2 = %p, new3 = %p\n", (void *)&ac, (void *)&new, &new2, &new3);
    pac = &ac;

    vListInitialise(&ac);

    vListInsertEnd(pac, &new);
	vListInsertEnd(pac, &new2);
	vListInsertEnd(pac, &new3);
	
	uxListRemove(&new2);
}
